package pt.isec.gpsgroup5.moneybox;

import java.util.Calendar;


public class Objective {
	
	private int id = -1;
	private String name = null;
	private double finalAmount = -1;
	private double currentAmount = -1;
	private int priority = -1;
	private int status = -1; 
	private int year = -1;
	private int month = -1;
	private int day = -1;
	
	final static int maxNameLength = 50;
	final static int minPriority = 1;
	final static int maxPriority = 3;
	final static int maxYear = 2099;
	final static double maxAmount = 1000000;
	final static int minStatus = 0;
	final static int maxStatus = 1;
	 
	
	
	//Constructor
	public Objective(int id, String name, double finalAmount, double currentAmount, int year, int month, int day, int priority, int status) {
		this.setId(id);
		this.setName(name);
		this.setFinalAmount(finalAmount);
		this.setCurrentAmount(currentAmount);
		this.setPriority(priority);
		this.setYear(year);
		this.setMonth(month);
		this.setDay(day);
		this.setStatus(status);
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		//If name's length is greater or equal than "maxNameLength"
		//the excess gets ignored
		if(name.length() >= maxNameLength) {
			name.substring(0, maxNameLength);
		}
		this.name = name;
	}

	/**
	 * @return the priority
	 */
	public int getPriority() {
		return priority;
	}

	/**
	 * @param priority the priority to set
	 */
	public void setPriority(int priority) {
		if (priority >= minPriority && priority <= maxPriority ) {
			this.priority = priority;
		}
	}

	/**
	 * @return the status
	 */
	public int getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(int status) {
		if(status >= minStatus && status <= maxStatus) {
			this.status = status;
		}
	}
	

	/**
	 * @return the finalAmount
	 */
	public double getFinalAmount() {
		return finalAmount;
	}

	/**
	 * @return the year
	 */
	public int getYear() {
		return year;
	}

	/**
	 * @param year the year to set
	 */
	public void setYear(int year) {
		int currentYear = Calendar.getInstance().get(Calendar.YEAR);
		if (year >= currentYear && year <= maxYear) {
			this.year = year;
		}
	}

	/**
	 * @return the month
	 */
	public int getMonth() {
		return month;
	}

	/**
	 * @param month the month to set
	 */
	public void setMonth(int month) {
		if (month >= 1 && month <= 12) {
			this.month = month;
		}
	}

	/**
	 * @return the day
	 */
	public int getDay() {
		return day;
	}

	/**
	 * @param day the day to set
	 */
	public void setDay(int day) {
		Calendar calendar = Calendar.getInstance();
		int maxDay = -1;
		
		if (year != -1 && month != -1) {
			calendar.set(year, month, 1);
			maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
			
			if(day >= 1 && day <= maxDay) {
				this.day = day;
			}	
		}
		
		
	}

	/**
	 * @param finalAmount the finalAmount to set
	 */
	public void setFinalAmount(double finalAmount) {
		if (finalAmount > 0 && finalAmount < maxAmount) {
			this.finalAmount = finalAmount;
		}
	}

	/**
	 * @return the currentAmount
	 */
	public double getCurrentAmount() {
		return currentAmount;
	}

	/**
	 * @param currentAmount the currentAmount to set
	 */
	public void setCurrentAmount(double currentAmount) {
		if (currentAmount >= 0 && currentAmount < maxAmount) {
			this.currentAmount = currentAmount;
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		if(id > 0) {
			this.id = id;
		}
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Objective [id=" + id + ", name=" + name + ", finalAmount="
				+ finalAmount + ", currentAmount=" + currentAmount
				+ ", priority=" + priority + ", status=" + status + ", year="
				+ year + ", month=" + month + ", day=" + day
				+ ", maxNameLength=" + maxNameLength + ", minPriority="
				+ minPriority + ", maxPriority=" + maxPriority + ", maxYear="
				+ maxYear + ", maxAmount=" + maxAmount + ", minStatus="
				+ minStatus + ", maxStatus=" + maxStatus + "]";
	}


	
	
}
